<?php

namespace App\Http\Controllers\Admin\AdminUsers;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\AdminUsers\Api\AdminUsersApi;
use App\Model\AdminUsers;
use App\Model\Role;
use App\Model\Permission;

/**
 * @author mais <[<380354618@qq.com>]>
 * [用于加载管理员管理模块的视图]
 */
class adminUsersController extends Controller
{
    //加载管理员管理->角色管理的视图
    public function role()
    {
        $res = Role::get();

    	return view('Admin/AdminUsers/role', ['list'=>$res]);
    }

    //用户角色添加页面
    public function roleAdd()
    {
        $list = Permission::get();
        return view('Admin/AdminUsers/roleadd', ['list'=>$list]);
    }

    //添加角色
    public function roleDoAdd(Request $request)
    {
        //添加角色
        $rolemod = new Role();

        $rolemod->name = $request->ename;
        $rolemod->display_name = $request->name;
        $rolemod->description = $request->description;

        $res = $rolemod->save();

        //添加权限
        $permission = [];
        foreach ($request->input('premission') as $key => $value) {
            $permission[] = ['permission_id' => $value, 'role_id' => $rolemod->id];
        }

        DB::table('permission_role')->insert($permission);
        
        if ($res) {
            return 1;
        }else{
            return 2;
        }
        
    }

    //更改用户角色页面
    public function roleedit()
    {
        $permission = Permission::get();

        $role = Role::find($_GET['id']);

        return view('Admin/AdminUsers/roleedit', ['permission'=>$permission], ['role'=>$role]);
    }

    //更改用户角色
    public function roleDoEdit(Request $request)
    {
        // dd($request->all());

        //修改角色
        $rolemod = Role::find($request->id);

        $rolemod->name = $request->ename;
        $rolemod->display_name = $request->name;
        $rolemod->description = $request->description;

        $res = $rolemod->save();

        //修改权限
        $role_permission = [];
        foreach ($request->input('premission') as $key => $value) {
            $role_permission[] = ['role_id' => $rolemod->id, 'permission_id' => $value];
        }

        //先把这个账号原本的角色全部删除
        DB::delete('delete from permission_role where role_id = ?',[$rolemod->id]);
        //在吧这次操作的角色添加上去
        DB::table('permission_role')->insert($role_permission);
        
        if ($res) {
            return 1;
        }else{
            return 2;
        }

    }

    public function roleDel(Request $request)
    {

        //先把于用户关联删除
        DB::delete('delete from admin_users_role where role_id = ?',[$request->id]);

        //再把与权限关联删除
        DB::delete('delete from permission_role where role_id = ?',[$request->id]);

        //最后删除roles表
        $res = DB::delete('delete from roles where id = ?',[$request->id]);

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    public function rolefind(Request $request)
    {

        $uname = $request->input('name');

        $uname = "%".$uname."%";

        $list = Role::where('display_name', 'like', $uname)->get();
        
        return view('../Admin/AdminUsers/rolefind', ['list'=>$list]);
    }

    public function roleshow(Request $request)
    {
        $mod =Role::find($request->id);

        return view('Admin/AdminUsers/roleshow', ['list'=>$mod]);
    }

    //加载管理员管理->权限管理的视图
    public function permission()
    {
        $res = Permission::get();

    	return view('Admin/AdminUsers/permission', ['list'=>$res]);
    }

    //加载添加权限管理页面
    public function permissionAdd()
    {
        return view('Admin/AdminUsers/permissionadd');
    }

    //添加权限
    public function permissionDoAdd(Request $request)
    {
        $permissionmod = new Permission();

        $permissionmod->name = $request->pathname;
        $permissionmod->display_name = $request->display_name;
        $permissionmod->description = $request->description;

        $res = $permissionmod->save();

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    //加载更改权限页面
    public function permissionEdit()
    {
        $permission = Permission::find($_GET['id']);

        return view('Admin/AdminUsers/permissionedit', ['permission'=>$permission]);
    }

    //修改权限
    public function permissionDoEdit(Request $request)
    {
        $permissionmod = Permission::find($request->id);

        $permissionmod->name = $request->pathname;
        $permissionmod->display_name = $request->display_name;
        $permissionmod->description = $request->description;

        $res = $permissionmod->save();

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    //删除权限
    public function permissiondel(Request $request)
    {
        //先把关联表的这个权限删除
        DB::delete('delete from permission_role where permission_id = ?',[$request->id]);

        //再把这个权限删除
        $res = DB::delete('delete from permissions where id = ?',[$request->id]);

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    public function findpermission(Request $request)
    {
        $uname = $request->input('name');

        $uname = "%".$uname."%";

        $list = Permission::where('display_name', 'like', $uname)->get();

        // dd($list);
        
        return view('../Admin/AdminUsers/permissionfind', ['list'=>$list]);
    }

    //加载管理员管理->管理员列表视图
    public function lists()
    {
        

        $users = Adminusers::paginate(10);

    	return view('Admin/AdminUsers/lists', ['list'=>$users]);
    }

    //加载管理员管理->管理员列表->添加管理员视图
    public function admin_add()
    {
        $roles = Role::get();
        return view('Admin/AdminUsers/admin_add', ['list'=>$roles]);
    }

    //加载管理员管理->管理员列表->添加管理员->ajax添加管理员
    public function doAdd(Request $request)
    {
        $usermod = new Adminusers();

        $usermod->uname = $request->adminName;
        $usermod->pass = md5(md5($request->password));
        $usermod->tel = $request->phone;
        $usermod->email = $request->email;
        $usermod->title = 1;
        $usermod->addtime = time();
        $usermod->state = 0;

        $res = $usermod->save();

        //添加角色
        //
        if ($request->input('roles')) {
            $role_user = [];
            foreach ($request->input('roles') as $key => $value) {
                $role_user[] = ['role_id' => $value, 'admin_users_id' => $usermod->id];
            }

            DB::table('admin_users_role')->insert($role_user);
        }
        
        

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    //加载管理员管理->管理员列表->修改
    public function edit()
    {
        $role = Role::get();

        $list = AdminUsers::find($_GET['id']);

        return view('Admin/AdminUsers/admin_edit', ['list'=>$list], ['role'=>$role]);
    }

    //加载管理员管理->管理员列表->添加管理员->修改管理员
    public function doedit(Request $request)
    {

        $usermod = Adminusers::find($request->id);

        $usermod->pass = md5(md5($request->password));
        $usermod->tel = $request->phone;
        $usermod->email = $request->email;
        $usermod->title = 1;

        $res = $usermod->save();

        //修改角色
        $role_user = [];
        foreach ($request->input('roles') as $key => $value) {
            $role_user[] = ['role_id' => $value, 'admin_users_id' => $usermod->id];
        }

        //先把这个账号原本的角色全部删除
        DB::delete('delete from admin_users_role where admin_users_id = ?',[$usermod->id]);
        //在吧这次操作的角色添加上去
        DB::table('admin_users_role')->insert($role_user);
        
        if ($res) {
            return 1;
        }else{
            return 2;
        }

    }

    //搜索用户
    public function finduser(Request $request)
    {

        $uname = $request->input('name');

        $uname = "%".$uname."%";

        $list = AdminUsers::where('uname', 'like', $uname)->paginate(10);
        
        return view('Admin/AdminUsers/listsfind', ['list'=>$list]);
    }
}
